package com.besttone.action;

import java.util.ArrayList;
import java.util.List;

import com.besttone.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import com.besttone.bean.IntergrationInfo;
import com.besttone.enums.EnumAPIResultCode;
import com.jfinal.core.Controller;

/**
 * 实时综合查询总数接口(短信数、短链接访问数、消息数)
 * http://localhost:8080/signalAndsms_2_solr/intergrationCount/queryAllCount
 */
public class IntegrationCountAction extends Controller {
    private static Log _log = LogFactory.getLog(IntegrationCountAction.class);

    public void queryAllCount() {
	String streaming_no = getPara("streaming_no");
	setAttr("streaming_no", streaming_no);
	String sp_account = getPara("sp_account");
		String sendPwd = getPara("sendPwd");//密码：MD5(streaming_no&sp_account&"sms")
	String intergration_info = getPara("intergration_info");
	String current_page = StringUtils.isBlank(getPara("current_page")) ? "1"
		: getPara("current_page");// 当前页数
	String per_page_size = StringUtils.isBlank(getPara("per_page_size")) ? "10"
		: getPara("per_page_size");// 每页条数，默认10
        StringBuffer pwdBuf=new StringBuffer();
        String nowpwd= MD5Util.md5(pwdBuf.append(streaming_no).append("&").append(sp_account)
                .append("&").append("sms").toString());
	_log.info("请求查询参数:streaming_no=" + streaming_no + ",sp_account="
		+ sp_account + ",intergration_info=" + intergration_info+",sendPwd="+sendPwd+",nowpwd="+nowpwd);
        if(!nowpwd.equals(sendPwd)){
            setAttr("result_code",
                    EnumAPIResultCode.UNAUTHORIZED_BUSINESS.getValue());
            setAttr("result_msg", "鉴权失败:密码错误");
            renderJson();
            return;
        }

        if (StringUtils.isBlank(sp_account)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "账号不能为空");//账号为空会躲避密码验证
            renderJson();
            return;
        }
	if (StringUtils.isBlank(intergration_info)) {
	    setAttr("result_code",
		    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
	    setAttr("result_msg", "集成查询信息参数不能为空");
	    renderJson();
	    return;
	}
	try {
	    List<Object> infoList = JsonUtil.jsonStringToObject(intergration_info, List.class);
	    List<IntergrationInfo> resultInfoList = new ArrayList<IntergrationInfo>();
	    for (Object obj : infoList) {
		IntergrationInfo info = JsonUtil.jsonStringToObject(
			obj.toString(), IntergrationInfo.class);
		if (StringUtils.isBlank(info.getTemplate_code())) {
		    setAttr("result_code",
			    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
		    setAttr("result_msg", "template_code参数不能为空");
		    renderJson();
		    return;
		}
		// query sms
		StringBuffer strBufSms = new StringBuffer();
		strBufSms.append("template_code:" + info.getTemplate_code());
		String querySql = strBufSms.toString();
		_log.info("solr查询sms语句:" + querySql);
		try {
		    SolrDocumentList docList = Sms2SolrUtil.search(querySql,
			    Integer.valueOf(current_page),
			    Integer.valueOf(per_page_size));
		    info.setSms_count(docList.getNumFound());
		} catch (Exception e) {
		    _log.info("solr查询异常:" + e);
		    setAttr("result_code",
			    EnumAPIResultCode.SYSTEM_ERROR.getValue());
		    setAttr("result_msg",
			    EnumAPIResultCode.SYSTEM_ERROR.getName());
		}
		// query sms end
		// query shortlink start
		StringBuffer strBufLink = new StringBuffer();
		strBufLink.append("template_code:" + info.getTemplate_code());
		_log.info("solr查询shortlink语句:" + strBufLink.toString());
		try {
		    SolrDocumentList docList = ShortlinkBrowseRecord2SolrUtil
			    .search(strBufLink.toString(),
				    Integer.valueOf(current_page),
				    Integer.valueOf(per_page_size));
		    info.setShortlink_count(docList.getNumFound());
		} catch (Exception e) {
		    _log.info("solr查询异常:" + e);
		    setAttr("result_code",
			    EnumAPIResultCode.SYSTEM_ERROR.getValue());
		    setAttr("result_msg",
			    EnumAPIResultCode.SYSTEM_ERROR.getName());
		}
		// query shortlink end

		// query coupon or reserve
            info.setMsg_count(0L);//暂时屏蔽此类数据，默认0
		/**if (StringUtils.isNotBlank(info.getCoupon_code())) {
		    StringBuffer strBufCoupon = new StringBuffer();
		    strBufCoupon.append("coupon_code:" + info.getCoupon_code());
		    _log.info("solr查询coupon语句:" + strBufCoupon.toString());
		    try {
			SolrDocumentList docList = ReceiveCoupon2SolrUtil
				.search(strBufCoupon.toString(),
					Integer.valueOf(current_page),
					Integer.valueOf(per_page_size));
			info.setMsg_count(docList.getNumFound());
		    } catch (Exception e) {
			_log.info("solr查询异常:" + e);
			setAttr("result_code",
				EnumAPIResultCode.SYSTEM_ERROR.getValue());
			setAttr("result_msg",
				EnumAPIResultCode.SYSTEM_ERROR.getName());
		    }
		}
		if (StringUtils.isNotBlank(info.getReserve_code())) {
		    StringBuffer strBufReserve = new StringBuffer();
		    strBufReserve.append("reserve_code:"
			    + info.getReserve_code());
		    _log.info("solr查询reserve语句:" + strBufReserve.toString());

		    try {
			SolrDocumentList docList = ReceiveReserve2SolrUtil
				.search(strBufReserve.toString(),
					Integer.valueOf(current_page),
					Integer.valueOf(per_page_size));
			info.setMsg_count(docList.getNumFound());
		    } catch (Exception e) {
			_log.info("solr查询异常:" + e);
			setAttr("result_code",
				EnumAPIResultCode.SYSTEM_ERROR.getValue());
			setAttr("result_msg",
				EnumAPIResultCode.SYSTEM_ERROR.getName());
		    }
		}**/
		resultInfoList.add(info);
	    }
	    // return
	    setAttr("intergration_info",
		    JsonUtil.ObjectToJsonString(resultInfoList));
	    setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
	    setAttr("result_msg", EnumAPIResultCode.SUCCESS.getName());
	    renderJson();
	    _log.info("return value:" + getAttr("intergration_info"));
	    _log.info("query intergration success");
	    return;
	} catch (Exception e) {
	    _log.info("exception:" + e);
	    setAttr("result_code", EnumAPIResultCode.BUSINESS_ERROR.getValue());
	    setAttr("result_msg", "集成查询信息格式错误");
	    renderJson();
	    return;
	}
    }
}
